package ihistoryApp.dal.controller;

import cn.hutool.json.JSONUtil;
import ihistoryApp.dal.entity.User;
import ihistoryApp.request.*;
import ihistoryApp.service.LoginService;
import ihistoryApp.service.RegisterService;
import ihistoryApp.view.UserView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/api/users")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ResponseEntity<UserView> loginUser(@RequestBody LoginRequest request) {
        UserView userView = loginService.loginRequest(request);
        return ResponseEntity.ok(userView);
    }

    @PostMapping("/register")
    public ResponseEntity<Long> register(@RequestBody RegisterRequest request) {
        return ResponseEntity.ok(loginService.register(request));
    }

    @PostMapping("/getValidateCode")
    public ResponseEntity<Boolean> getValidateCode(@RequestBody GetValidateCodeRequest request) {
        return ResponseEntity.ok(loginService.getRegisterValidateCode(request));
    }

    @PostMapping("/updatePassword")
    public ResponseEntity<Long> updatePassword(@RequestBody UpdatePasswordRequest request) {
        return ResponseEntity.ok(loginService.updatePassword(request));
    }

    @PostMapping("/getUpdatePwdValidateCode")
    public ResponseEntity<Boolean> getUpdatePwdValidateCode(@RequestBody GetValidateCodeRequest request) {
        return ResponseEntity.ok(loginService.getUpdatePwdValidateCode(request));
    }

    @PostMapping("/getUserInfo")
    public ResponseEntity<UserView> getUserInfo(@RequestBody GetUserInfoRequest request) {
        return ResponseEntity.ok(loginService.getUserInfo(request));
    }

    @PostMapping("/updateUserInfo")
    public ResponseEntity<Boolean> updateUserInfo(@RequestBody UpdateUserInfoRequest request) {
        return ResponseEntity.ok(loginService.updateUserInfo(request));
    }

}